﻿<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <title>Replace intervals</title>
    <link href="Styles/js-console.css" rel="stylesheet" />
    <script src="Scripts/isValidInput.js"></script>
</head>
<body>
    <div id="task">
        <p>
            Write a function that replaces non breaking white-spaces in a text with &nbsp;
        </p>
        <a href="4.html">Previous task</a>
        <a href="6.html">Next task</a>
    </div>

    <br />
    <label for="inputText">Enter text:</label>
    <input type="text" id="inputText" />
    <button onclick="replaceNbsp()">Run script</button>

    <div id="js-console"></div>
    <script src="Scripts/js-console.js"></script>

    <script>
        function replaceNbsp() {

            var inputText = document.getElementById("inputText").value;
            var nbsp = "&nbsp;"
            var comparerForConsole = "-";

            var text = inputText;
            text = reaplaceWhitespaceWith(text, nbsp);
            jsConsole.writeLine(text);

            //for comparing in the console
            text = inputText;
            text = reaplaceWhitespaceWith(text, comparerForConsole);
            jsConsole.writeLine(text);
        }

        function reaplaceWhitespaceWith(text, replacer) {

            var index = text.indexOf("  ");
            while (index != -1) {
                text = text.replace("  ", replacer);
                text = text.replace(replacer," ");
                index = text.indexOf("  ");
            }
            return text;
        }
    </script>
</body>
</html>
